package com.example.demo.controller;

import com.example.demo.models.TestEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/test")
public class DemoController {

    @PostMapping("/test1")
    public Mono<TestEntity> add(@RequestBody TestEntity entity) {
        return Mono.justOrEmpty(entity);
    }

    @GetMapping("/test2")
    public Flux<TestEntity> findAll() {
        List<TestEntity> list = new ArrayList<>();
        list.add(TestEntity.builder().id(1).username("name1").address("address1...").build());
        list.add(TestEntity.builder().id(2).username("name2").address("address2...").build());
        list.add(TestEntity.builder().id(3).username("name3").address("address3...").build());
        return Flux.fromIterable(list);
    }

    @GetMapping(value = "test3", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<TestEntity> streamFindAll() {
        List<TestEntity> list = new ArrayList<>();
        list.add(TestEntity.builder().id(1).username("name1").address("address1...").build());
        list.add(TestEntity.builder().id(2).username("name2").address("address2...").build());
        list.add(TestEntity.builder().id(3).username("name3").address("address3...").build());
        return Flux.fromIterable(list);
    }

    @DeleteMapping("/test4/{id}")
    public Mono<ResponseEntity<Boolean>> delete(@RequestParam(name = "id") @PathVariable String id) {
        return Mono.just(new ResponseEntity<>(Boolean.TRUE, HttpStatus.OK));
    }

    @PutMapping("/test5")
    public Mono<ResponseEntity<TestEntity>> update(@RequestBody TestEntity entity) {
        return Mono.just(new ResponseEntity<>(new TestEntity(1, "name1", "address1..."), HttpStatus.OK));
    }

}
